package com.etoc.test.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptUtil {
	public final static String MD5 = "MD5";
	public final static String SHA = "SHA";
	public final static String AES = "AES";
	public final static String DES = "DES";
	public final static String CHARSET_NAME = "UTF-8";
	/**
	 * 
	 */
	public final static String CBC_MOB = "/CBC/PKCS5Padding";
	public final static String ECB_MOB = "/ECB/PKCS5Padding";
	public final static String AES_VEC = "0123456789abcdef";
	public final static String DES_VEC = "01234567";
	
	/**
	 * 
	 * 
	 * @param source
	 * @return
	 * @see 
	 */
	public static String encryptMD5(String source) {
		try {
			MessageDigest md5 = MessageDigest.getInstance(MD5);
			md5.update(source.getBytes(CHARSET_NAME));
			byte[] bytes = md5.digest();
			if (bytes == null || bytes.length <= 0) {  
				return null;  
			}
			return encodeByteToHexStr(bytes).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param source
	 * @return
	 * @see 
	 */
	public static String encryptSHA(String source) {
		try {
			MessageDigest sha = MessageDigest.getInstance(SHA);  
			sha.update(source.getBytes(CHARSET_NAME));
			byte[] bytes = sha.digest();
			if (bytes == null || bytes.length <= 0) {  
				return null;  
			}
	        return encodeByteToHexStr(bytes).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param source
	 * @return
	 * @see 
	 */
	public static String encryptBASE64(String source) {
		BASE64Encoder encoder = new BASE64Encoder();
		try {
			String encodeStr = encoder.encodeBuffer(source.getBytes(CHARSET_NAME));
			return filter(encodeStr);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param decryptStr
	 * @return
	 * @see 
	 */
	public static String decryptBASE64(String decryptStr) {
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] decodeBytes = decoder.decodeBuffer(decryptStr);
			return new String(decodeBytes, CHARSET_NAME);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param source
	 * @param password 
	 * @param vector 
	 * @return
	 * @see 
	 */
	public static String encryptAES(String source, String password, String vector) {
		try{
			if(password == null || password.getBytes(CHARSET_NAME).length != 16) {
				return null;
			}
			boolean flag = false;
			if (vector != null && vector.getBytes(CHARSET_NAME).length == 16) {
				flag = true;
			}
			Cipher enCipher;
			if (flag) {
				enCipher = Cipher.getInstance(AES + CBC_MOB);
				Key key = getKey(password, AES);
				IvParameterSpec zeroIv = new IvParameterSpec(vector.getBytes(CHARSET_NAME));
				enCipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
			} else {
				enCipher = Cipher.getInstance(AES + ECB_MOB);
				Key key = getKey(password, AES);
				enCipher.init(Cipher.ENCRYPT_MODE, key);
			}
			byte[] bytes = source.getBytes(CHARSET_NAME);
			byte[] pasByte = enCipher.doFinal(bytes);
			return encodeByteToHexStr(pasByte).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param decryptStr
	 * @param password 
	 * @param vector 
	 * @return
	 * @see 
	 */
	public static String decryptAES(String decryptStr, String password, String vector) {
		try {
			if(password == null || password.getBytes(CHARSET_NAME).length != 16) {
				return null;
			}
			boolean flag = false;
			if (vector != null && vector.getBytes(CHARSET_NAME).length == 16) {
				flag = true;
			}
			Cipher deCipher;
			if (flag) {
				deCipher = Cipher.getInstance(AES + CBC_MOB);
				Key key = getKey(password, AES);
				IvParameterSpec zeroIv = new IvParameterSpec(vector.getBytes(CHARSET_NAME));
				deCipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
			} else {
				deCipher = Cipher.getInstance(AES + ECB_MOB);
				Key key = getKey(password, AES);
				deCipher.init(Cipher.DECRYPT_MODE, key);
			}
			byte[] decryptBytes = decodeHexStrToByte(decryptStr);
			byte[] parBytes = deCipher.doFinal(decryptBytes);
			return new String(parBytes, CHARSET_NAME);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param source
	 * @param password 
	 * @param vector 
	 * @return
	 * @see 
	 */
	public static String encryptDES(String source, String password, String vector) {
		try{
			if(password == null || password.getBytes(CHARSET_NAME).length != 8) {
				return null;
			}
			boolean flag = false;
			if (vector != null && vector.getBytes(CHARSET_NAME).length == 8) {
				flag = true;
			}
			Cipher enCipher;
			if (flag) {
				enCipher = Cipher.getInstance(DES + CBC_MOB);
				Key key = getKey(password, DES);
				IvParameterSpec zeroIv = new IvParameterSpec(vector.getBytes(CHARSET_NAME));
				enCipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
			} else {
				enCipher = Cipher.getInstance(DES + ECB_MOB);
				Key key = getKey(password, DES);
				enCipher.init(Cipher.ENCRYPT_MODE, key);
			}
			byte[] bytes = source.getBytes(CHARSET_NAME);
			byte[] pasByte = enCipher.doFinal(bytes);
			return encodeByteToHexStr(pasByte).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * 
	 * 
	 * @param decryptStr
	 * @param password 
	 * @param vector 
	 * @return
	 * @see 
	 */
	public static String decryptDES(String decryptStr, String password, String vector) {
		try {
			if(password == null || password.getBytes(CHARSET_NAME).length != 8) {
				return null;
			}
			boolean flag = false;
			if (vector != null && vector.getBytes(CHARSET_NAME).length == 8) {
				flag = true;
			}
			Cipher deCipher;
			if (flag) {
				deCipher = Cipher.getInstance(DES + CBC_MOB);
				Key key = getKey(password, DES);
				IvParameterSpec zeroIv = new IvParameterSpec(vector.getBytes(CHARSET_NAME));
				deCipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
			} else {
				deCipher = Cipher.getInstance(DES + ECB_MOB);
				Key key = getKey(password, DES);
				deCipher.init(Cipher.DECRYPT_MODE, key);
			}
			byte[] decryptBytes = decodeHexStrToByte(decryptStr);
			byte[] parBytes = deCipher.doFinal(decryptBytes);
			return new String(parBytes, CHARSET_NAME);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param source
	 * @return
	 * @see 
	 */
	private static String filter(String source) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < source.length(); i++) {
			int ascii = source.charAt(i);
			if (ascii != 10 && ascii != 13) {
				sb.append(source.subSequence(i, i + 1));
			}
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * 
	 * @param password
	 * @param algorithm
	 * @return
	 * @throws Exception
	 * @see 
	 */
	private static Key getKey(String password, String algorithm) throws Exception{
		byte[] keys = password.getBytes(CHARSET_NAME);
		SecretKeySpec skeySpec = new SecretKeySpec(keys, algorithm);
		return skeySpec;
	}
	
	/**
	 * 
	 * 
	 * @param buf
	 * @return
	 * @see 
	 */
	private static String encodeByteToHexStr(byte[] buf) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex);
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * 
	 * @param hexStr
	 * @return
	 * @see 
	 */
	private static byte[] decodeHexStrToByte(String hexStr) {
		if (hexStr.length() < 1) {
			return null;
		}
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}
	
	public static void main(String[] args) {
		String des = EncryptUtil.encryptBASE64("free");
		String src = EncryptUtil.decryptBASE64(des);
		
		/*String des = EncryptUtil.encryptAES("I love you", "�Ұ����л�a", "12����յ���");
		String src = EncryptUtil.decryptAES(des, "�Ұ����л�a", "12����յ���");*/
		
		/*String des = EncryptUtil.encryptDES("I love you", "cc199004", "12345678");
		String src = EncryptUtil.decryptDES(des, "cc199004", "12345678");*/
		
		System.out.println(des);
		System.out.println(src);
	}
}
